package com.s64.ch07.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.s64.ch07.entity.SysRole;
import com.s64.ch07.mapper.SysRoleMapper;
import com.s64.ch07.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.List;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<SysRole> list() {
        final String key = "s64:role:list";
        List<SysRole> list = (List<SysRole>) redisTemplate.opsForValue().get(key);
        if (list == null || list.isEmpty()) {
            list = super.list();
            redisTemplate.opsForValue().set(key, list, Duration.ofDays(10));
        }
        return list;
    }
}
